import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

// 路由分类
// 1.静态路由
export let staticRoute = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login.vue'),
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/Home.vue'),
    children: [],
  },
]

// 2.动态路由
// 第一种方式
// let dynamicRoute = []

// 第二种方式
// 定义异步路由
export const asyncRoute = [
  {
    path: 'user',
    name: 'User',
    meta: { title: '用户管理' },
    component: () => import('@/views/User'),
  },
  {
    path: 'role',
    name: 'Role',
    meta: { title: '角色管理' },
    component: () => import('@/views/Role'),
  },
  {
    path: 'menu',
    name: 'Menu',
    meta: { title: '菜单管理' },
    component: () => import('@/views/Menu'),
  },
  {
    path: 'addTeacher',
    name: 'AddTeacher',
    meta: { title: '添加讲师' },
    component: () => import('@/views/AddTeacher'),
  },
  {
    path: 'teacherList',
    name: 'TeacherList',
    meta: { title: '讲师列表' },
    component: () => import('@/views/TeacherList'),
  },
  {
    path: 'categoryList',
    name: 'CategoryList',
    meta: { title: '课程分类列表' },
    component: () => import('@/views/CategoryList'),
  },
  {
    path: 'addCategory',
    name: 'AddCategory',
    meta: { title: '添加课程' },
    component: () => import('@/views/AddCategory'),
  },
  {
    path: 'courseList',
    name: 'CourseList',
    meta: { title: '课程列表' },
    component: () => import('@/views/CourseList'),
  },
  {
    path: 'publicCourse',
    name: 'PublicCourse',
    meta: { title: '发布课程' },
    component: () => import('@/views/PublicCourse'),
  },
]

// 404路由
export let exceptionRoute = [
  {
    path: '*',
    component: () => import('@/views/404.vue'),
  },
]

const router = new VueRouter({
  routes: staticRoute,
})

router.beforeEach(function (to, from, next) {
  if (to.path === '/login') {
    next()
  } else if (to.path.includes('/home') && sessionStorage.getItem('state')) {
    next()
  } else {
    next('/login')
  }
})

export default router
